<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateCooperationAPIRequest;
use App\Http\Requests\API\UpdateCooperationAPIRequest;
use App\Models\Cooperation;
use App\Repositories\CooperationRepository;
use App\Repositories\ChannelRepository;
use App\Repositories\ItemRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class CooperationController
 * @package App\Http\Controllers\API
 */

class CooperationAPIController extends AppBaseController
{
    /** @var  CooperationRepository */
    private $cooperationRepository;
    private $channelRepository;
    private $itemRepository;

    public function __construct(CooperationRepository $cooperationRepo,
        ChannelRepository $channelRepo,
        ItemRepository $itemRepo)
    {
        $this->cooperationRepository = $cooperationRepo;
        $this->channelRepository = $channelRepo;
        $this->itemRepository = $itemRepo;
    }

    /**
     * @param Request $request
     * @return Response
     *
     * @SWG\Get(
     *      path="/cooperations",
     *      summary="Get a listing of the Cooperations.",
     *      tags={"Cooperation"},
     *      description="Get all Cooperations",
     *      produces={"application/json"},
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(ref="#/definitions/Cooperation")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $this->cooperationRepository->pushCriteria(new RequestCriteria($request));
        $this->cooperationRepository->pushCriteria(new LimitOffsetCriteria($request));
        $cooperations = $this->cooperationRepository->all();

        return $this->sendResponse($cooperations->toArray(), trans('flash.api.retrieve_success', ['model' => 'Cooperations']));
    }

    /**
     * @param CreateCooperationAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/cooperations",
     *      summary="Store a newly created Cooperation in storage",
     *      tags={"Cooperation"},
     *      description="Store Cooperation",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="Cooperation that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/Cooperation")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/Cooperation"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateCooperationAPIRequest $request)
    {
        $input = $request->all();

        $channelId = $request->input('channel_id');
        $channel = $this->channelRepository->findWithoutFail($channelId);
        if (empty($channel))
        {
            return $this->sendError(trans('flash.api.not_found', ['model' => 'Channel']), 404);
        }

        $itemId = $request->input('item_id');
        $item = $this->itemRepository->findWithoutFail($itemId);
        if (empty($item))
        {
            return $this->sendError(trans('flash.api.not_found', ['model' => 'Item']), 404);
        }

        $cooperation = $this->cooperationRepository->getByChannelIdAndItemId($channelId, $itemId);
        if ($cooperation)
        {
            return $this->sendError('已提交过合作申请');
        }

        $cooperations = $this->cooperationRepository->create($input);

        return $this->sendResponse($cooperations->toArray(), trans('flash.api.save_success', ['model' => 'Cooperation']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/cooperations/{id}",
     *      summary="Display the specified Cooperation",
     *      tags={"Cooperation"},
     *      description="Get Cooperation",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of Cooperation",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/Cooperation"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var Cooperation $cooperation */
        $cooperation = $this->cooperationRepository->findWithoutFail($id);

        if (empty($cooperation))
        {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'Cooperation']), 404);
        }

        return $this->sendResponse($cooperation->toArray(), trans('flash.api.retrieve_success', ['model' => 'Cooperation']));
    }

    /**
     * @param int $id
     * @param UpdateCooperationAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/cooperations/{id}",
     *      summary="Update the specified Cooperation in storage",
     *      tags={"Cooperation"},
     *      description="Update Cooperation",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of Cooperation",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="Cooperation that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/Cooperation")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/Cooperation"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateCooperationAPIRequest $request)
    {
        $input = $request->all();

        /** @var Cooperation $cooperation */
        $cooperation = $this->cooperationRepository->findWithoutFail($id);

        if (empty($cooperation))
        {
            return $this->sendError(trans('flash.api.not_found', ['model' => 'Cooperation']), 404);
        }
        elseif ($cooperation->status == Cooperation::STATUS_SUCCESS && !empty($input['agree']))
        {
            return $this->sendError('该请求已经处理');
        }
        elseif ($cooperation->status == Cooperation::STATUS_OPEN && !empty($input['open']))
        {
            return $this->sendError('该请求已经处理');
        }

        if ( ! empty($input['agree']) && $cooperation->status == Cooperation::STATUS_DEFAULT)
        {
            $cooperation->status = Cooperation::STATUS_SUCCESS;
        }
        elseif ( ! empty($input['open']) && $cooperation->status == Cooperation::STATUS_SUCCESS)
        {
            $cooperation->status = Cooperation::STATUS_OPEN;
        }
        else
        {
            return $this->sendError('该请求已经处理');
        }

        $cooperation->save();

        return $this->sendResponse($cooperation->toArray(), trans('flash.api.update_success', ['model' => 'Cooperation']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/cooperations/{id}",
     *      summary="Remove the specified Cooperation from storage",
     *      tags={"Cooperation"},
     *      description="Delete Cooperation",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of Cooperation",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="string"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function destroy($id)
    {
        /** @var Cooperation $cooperation */
        $cooperation = $this->cooperationRepository->findWithoutFail($id);

        if (empty($cooperation))
        {
            return $this->sendError('Cooperation not found', 404);
        }

        $cooperation->delete();

        return $this->sendResponse($id, trans('flash.api.delete_success', ['model' => 'Cooperation']));
    }
}
